package com.mgq.netty.chat.server;

import com.mgq.netty.chat.handler.RpcRequestMessageHandler;
import com.mgq.netty.chat.protocol.MessageCodecShare;
import com.mgq.netty.chat.protocol.ProtocolFrameDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;

public class RpcServer {
    public static void main(String[] args) throws InterruptedException {
        LoggingHandler LOG_HANDLER = new LoggingHandler();
        MessageCodecShare MES_CODEC = new MessageCodecShare();
        RpcRequestMessageHandler RPC_REQUEST_HANDLER = new RpcRequestMessageHandler();
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        ChannelFuture future = new ServerBootstrap()
                .group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ProtocolFrameDecoder());
                        ch.pipeline().addLast(LOG_HANDLER);
                        ch.pipeline().addLast(MES_CODEC);
                        ch.pipeline().addLast(RPC_REQUEST_HANDLER);

                    }
                }).bind(8080);
        future.sync();
    }

}
